package com.nextive.interview.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.nextive.interview.R;
import com.nextive.interview.service.UserInfoService;
import com.nextive.interview.service.impl.UserInfoServiceImpl;

public class InitialSetupActivity extends Activity {
    
	private UserInfoService userInfoService;
	
	// TODO - should we have these member fields ???
	private EditText nameInput;
	private EditText emailInput;
	private EditText optionsInput;
	private Button confirmButton;
	private Button recipientButton;
	
	public InitialSetupActivity() {
		
		this.userInfoService = new UserInfoServiceImpl(this);
		
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	super.onCreate(savedInstanceState);
    	if (userInfoService.isUserFirstAccess()) {
        	
        	configureUserFirstTimeLayout(savedInstanceState);
        	
        } else {
        	
        	forwardToListActivity();
        	
        }
    	
    }
    
    private void configureUserFirstTimeLayout(Bundle savedInstanceState) {

        setContentView(R.layout.initial_set_up);
        
        nameInput = (EditText) findViewById(R.id.name);
        emailInput = (EditText) findViewById(R.id.email);
        optionsInput = (EditText) findViewById(R.id.options);
        confirmButton = (Button) findViewById(R.id.confirm_setup);
        recipientButton = (Button) findViewById(R.id.recipient);
        
        configureRecipientButton();
        configureConfirmButton();

    }
    
    /**
     * It forwards current flow to list activity.
     */
    private void forwardToListActivity() {
    	
    	Intent intent = new Intent(this, ViewWeightListActivity.class);
    	startActivity(intent);
    	
    }

    /**
     * It is in charge of initializing recipient button. This means, add corresponding listener.
     */
    private void configureRecipientButton() {
    	
    	recipientButton.setOnClickListener(new OnClickListener() {
			
			// TODO - check fi we are going to have this button or not !
    		@Override
			public void onClick(View v) {
			
				
			}
		});
    }
    
    private void configureConfirmButton() {
    	
    	confirmButton.setOnClickListener(new OnClickListener() {
			
			// TODO - save actual info obtained from the view !
    		@Override
			public void onClick(View v) {
			
				String name = nameInput.getText().toString();
				String email = emailInput.getText().toString();
				String recipient = "somebody@email.com";
				String option = optionsInput.getText().toString();
				
    			// TODO - any validation on the email ???
				
				userInfoService.saveUserInfo(name, email, recipient, option);
				
				forwardToListActivity();
				
			}
		});
    	
    }
}